<?php

namespace App\Admin\Controllers;

use App\Models\Alarm;
use App\Models\Monitor;
use App\Models\Scene;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class MonitorController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('监控管理');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Monitor::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->disableExport();
            $grid->disableFilter();
            $grid->id('ID')->sortable();
            $grid->title('名称')->label();
            $grid->link('链接');
            $grid->scene_id('所属景点')->display(function ($val){
                return Scene::all()->pluck('title','id')[$val];
            });
            $grid->latlng('坐标');
            $grid->online('是否在线')->display(function ($val){
                $alarm = Alarm::where('channel_id',$this->channel_id)->first();
                return $alarm?'不在线':'在线';
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Monitor::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('title','名称')->rules('required');
            $form->number('channel_id','通道ID')->rules(function ($form) {
                // 如果不是编辑状态，则添加字段唯一验证
                if (!$channel_id = $form->model()->channel_id) {
                    return 'unique:monitors';
                }
            },['unique'=>'已存在该字段'])->help('输入通道ID则无需添加封面及链接');
            $form->select('scene_id','所属景区景点')->options(Scene::all()->pluck('title','id'))->rules('required');
            $form->image('cover','封面')->uniqueName()->removable();
            $form->text('link','链接');
            $form->tags('latlng','坐标');
            $form->saved(function (Form $form) {
                if($form->model()->channel_id!=0){
                    $channel_id = $form->model()->channel_id;
                    $form->model()->link=get_rtmp_link($channel_id);
                    $form->model()->hls=get_hls_link($channel_id);
                    $form->model()->cover=get_monitor_cover($channel_id);
                    $form->model()->save();
                }
            });
            $form->hidden('online')->default(1);
        });
    }
}
